package com.atguigu.gmall.product;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {
    public static void main(String[] args) throws Exception {
        //1.创建新线程
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, //核心线程
                10, //最大线程
                5, //闲置线程的存货时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                //工厂: 默认工厂
                //拒绝策略
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        //【强制】 不允许使用下面工具类
        //Executors.newCachedThreadPool();

        //开启新线程
        CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        }, threadPoolExecutor);


        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "123";
        },threadPoolExecutor);

        System.out.println(stringCompletableFuture.get());


    }
}
